package com.example.elmmerchants.Shop_Activity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Check_All_Activity_Page extends AppCompatActivity {

    LinearLayout admissions_button, order_volume_button, unit_value_button, repurchase_volume_button, full_reduction_button, reduce_the_delivery_fee_button, discount_product_button, coupon_button, new_custom_receive_a_discount_button;
    View admissions_button_view, order_volume_button_view, unit_value_button_view, repurchase_volume_button_view;
    TextView go_to_check_up_order, go_to_check_custom_money, go_to_check_repurchase_volume, admissions_button_text_view, order_volume_button_text_view, unit_value_button_text_view, repurchase_volume_button_text_view;
    GridLayout top;
    TextView last_text_view;
    View last_view;
    ScrollView scrollView;
    TextView full_reduction_activity_is_ongoing, reduce_the_delivery_fee_is_ongoing, discount_product_is_ongoing, coupon_is_ongoing, new_custom_receive_a_discount_is_ongoing;

    String email, shop_name;
    AlertDialog dialog;
    boolean is_finish_loading_data = false, is_open_collect_and_have_gifts_page = false, is_open_collect_points_to_return_coupon = false, is_open_return_coupon_for_placing_an_order = false;
    int full_reduction_activity_state = 0, reduce_the_delivery_fee_state = 0, discount_product_state = 0, coupon_state = 0, new_custom_receive_a_discount_state = 0;

    private class Data_Thread extends Thread {
        @Override
        public void run() {
            is_finish_loading_data = false;
            is_open_collect_and_have_gifts_page = false;
            is_open_collect_points_to_return_coupon = false;
            is_open_return_coupon_for_placing_an_order = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant_shop_full_reduction_activity WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set.getString("activity_end_day"));
                        Date today = new Date();
                        if (date.getTime() + 86400000 < today.getTime()) {
                            full_reduction_activity_state = 2;
                        }
                        else {
                            full_reduction_activity_state = 1;
                        }
                    }

                    String sql2 = "SELECT * FROM merchant_reduce_the_delivery_fee WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql2);
                    preparedStatement.setString(1, email);
                    ResultSet set2 = preparedStatement.executeQuery();
                    while (set2.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set2.getString("activity_end_day"));
                        Date today = new Date();
                        if (date.getTime() + 86400000 < today.getTime()) {
                            reduce_the_delivery_fee_state = 2;
                        }
                        else {
                            reduce_the_delivery_fee_state = 1;
                        }
                    }

                    String sql3 = "SELECT * FROM merchant_discount_product_page WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql3);
                    preparedStatement.setString(1, email);
                    ResultSet set3 = preparedStatement.executeQuery();
                    while (set3.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set3.getString("activity_end_day"));
                        Date today = new Date();
                        if (date.getTime() + 86400000 < today.getTime()) {
                            discount_product_state = 2;
                        }
                        else {
                            discount_product_state = 1;
                        }
                    }

                    String sql4 = "SELECT * FROM merchant_coupon_collect_and_get_give WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql4);
                    preparedStatement.setString(1, email);
                    ResultSet set4 = preparedStatement.executeQuery();
                    while (set4.next()) {
                        is_open_collect_and_have_gifts_page = true;
                        coupon_state += 1;
                    }

                    String sql5 = "SELECT * FROM merchant_coupon_collect_points_to_return_coupon WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql5);
                    preparedStatement.setString(1, email);
                    ResultSet set5 = preparedStatement.executeQuery();
                    while (set5.next()) {
                        is_open_collect_points_to_return_coupon = true;
                        coupon_state += 1;
                    }

                    String sql6 = "SELECT * FROM merchant_coupon_return_coupon_for_placing_an_order WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql6);
                    preparedStatement.setString(1, email);
                    ResultSet set6 = preparedStatement.executeQuery();
                    while (set6.next()) {
                        is_open_return_coupon_for_placing_an_order = true;
                        coupon_state += 1;
                    }

                    String sql7 = "SELECT * FROM merchant_new_custom_receive_a_discount WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql7);
                    preparedStatement.setString(1, email);
                    ResultSet set7 = preparedStatement.executeQuery();
                    while (set7.next()) {
                        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                        Date date = df.parse(set7.getString("activity_end_day"));
                        Date today = new Date();
                        if (date.getTime() + 86400000 < today.getTime()) {
                            new_custom_receive_a_discount_state = 2;
                        }
                        else {
                            new_custom_receive_a_discount_state = 1;
                        }
                    }
                    is_finish_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    if (full_reduction_activity_state == 0) {
                        full_reduction_activity_is_ongoing.setText("未创建");
                    }
                    else if (full_reduction_activity_state == 1) {
                        full_reduction_activity_is_ongoing.setText("已创建");
                    }
                    else {
                        full_reduction_activity_is_ongoing.setText("已过期");
                    }

                    if (reduce_the_delivery_fee_state == 0) {
                        reduce_the_delivery_fee_is_ongoing.setText("未创建");
                    }
                    else if (reduce_the_delivery_fee_state == 1) {
                        reduce_the_delivery_fee_is_ongoing.setText("已创建");
                    }
                    else {
                        reduce_the_delivery_fee_is_ongoing.setText("已过期");
                    }

                    if (discount_product_state == 0) {
                        discount_product_is_ongoing.setText("未创建");
                    }
                    else if (discount_product_state == 1) {
                        discount_product_is_ongoing.setText("已创建");
                    }
                    else {
                        discount_product_is_ongoing.setText("已过期");
                    }

                    if (coupon_state == 0) {
                        coupon_is_ongoing.setText("未创建");
                    }
                    else if (coupon_state == 3) {
                        coupon_is_ongoing.setText("已创建");
                    }
                    else if (coupon_state >= 1) {
                        coupon_is_ongoing.setText("已创建部分");
                    }

                    if (new_custom_receive_a_discount_state == 0) {
                        new_custom_receive_a_discount_is_ongoing.setText("未创建");
                    }
                    else if (new_custom_receive_a_discount_state == 1) {
                        new_custom_receive_a_discount_is_ongoing.setText("已创建");
                    }
                    else {
                        new_custom_receive_a_discount_is_ongoing.setText("已过期");
                    }
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_all_page);

        email = getIntent().getStringExtra("email");
        shop_name = getIntent().getStringExtra("shop_name");

        admissions_button = findViewById(R.id.admissions_button);
        admissions_button_view = findViewById(R.id.admissions_button_view);
        order_volume_button = findViewById(R.id.order_volume_button);
        order_volume_button_view = findViewById(R.id.order_volume_button_view);
        unit_value_button = findViewById(R.id.unit_value_button);
        unit_value_button_view = findViewById(R.id.unit_value_button_view);
        repurchase_volume_button = findViewById(R.id.repurchase_volume_button);
        repurchase_volume_button_view = findViewById(R.id.repurchase_volume_button_view);
        go_to_check_up_order = findViewById(R.id.go_to_check_up_order);
        go_to_check_custom_money = findViewById(R.id.go_to_check_custom_money);
        go_to_check_repurchase_volume = findViewById(R.id.go_to_check_repurchase_volume);
        top = findViewById(R.id.top);
        admissions_button_text_view = findViewById(R.id.admissions_button_text_view);
        order_volume_button_text_view = findViewById(R.id.order_volume_button_text_view);
        unit_value_button_text_view = findViewById(R.id.unit_value_button_text_view);
        repurchase_volume_button_text_view = findViewById(R.id.repurchase_volume_button_text_view);
        scrollView = findViewById(R.id.scrollView);
        full_reduction_button = findViewById(R.id.full_reduction_button);
        full_reduction_activity_is_ongoing = findViewById(R.id.full_reduction_activity_is_ongoing);
        reduce_the_delivery_fee_button = findViewById(R.id.reduce_the_delivery_fee_button);
        reduce_the_delivery_fee_is_ongoing = findViewById(R.id.reduce_the_delivery_fee_is_ongoing);
        discount_product_button = findViewById(R.id.discount_product_button);
        discount_product_is_ongoing = findViewById(R.id.discount_product_is_ongoing);
        coupon_button = findViewById(R.id.coupon_button);
        coupon_is_ongoing = findViewById(R.id.coupon_is_ongoing);
        new_custom_receive_a_discount_is_ongoing = findViewById(R.id.new_custom_receive_a_discount_is_ongoing);
        new_custom_receive_a_discount_button = findViewById(R.id.new_custom_receive_a_discount_button);

        set_dialog();
        Data_Thread data_thread = new Data_Thread();
        data_thread.start();

        last_view = admissions_button_view;
        last_text_view = admissions_button_text_view;

        admissions_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scrollView.smoothScrollTo((int) top.getX(), (int) top.getY());
                if (last_text_view != admissions_button_text_view) {
                    admissions_button_text_view.setTextColor(Color.BLACK);
                    admissions_button_text_view.setTextSize(18);
                    admissions_button_view.setVisibility(View.VISIBLE);
                    last_view.setVisibility(View.GONE);
                    last_text_view.setTextSize(17);
                    last_text_view.setTextColor(Color.rgb(128, 128, 128));
                    last_view = admissions_button_view;
                    last_text_view = admissions_button_text_view;
                }
            }
        });

        order_volume_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scrollView.smoothScrollTo((int) go_to_check_up_order.getX(), (int) go_to_check_up_order.getY());
                if (last_text_view != order_volume_button_text_view) {
                    order_volume_button_text_view.setTextColor(Color.BLACK);
                    order_volume_button_text_view.setTextSize(18);
                    order_volume_button_view.setVisibility(View.VISIBLE);
                    last_view.setVisibility(View.GONE);
                    last_text_view.setTextSize(17);
                    last_text_view.setTextColor(Color.rgb(128, 128, 128));
                    last_view = order_volume_button_view;
                    last_text_view = order_volume_button_text_view;
                }
            }
        });

        unit_value_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scrollView.smoothScrollTo(0, (int) go_to_check_custom_money.getY());
                if (last_text_view != unit_value_button_text_view) {
                    unit_value_button_text_view.setTextColor(Color.BLACK);
                    unit_value_button_text_view.setTextSize(18);
                    unit_value_button_view.setVisibility(View.VISIBLE);
                    last_view.setVisibility(View.GONE);
                    last_text_view.setTextSize(17);
                    last_text_view.setTextColor(Color.rgb(128, 128, 128));
                    last_view = unit_value_button_view;
                    last_text_view = unit_value_button_text_view;
                }
            }
        });

        repurchase_volume_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scrollView.smoothScrollTo(0, (int) go_to_check_repurchase_volume.getY());
                if (last_text_view != repurchase_volume_button_text_view) {
                    repurchase_volume_button_text_view.setTextColor(Color.BLACK);
                    repurchase_volume_button_text_view.setTextSize(18);
                    repurchase_volume_button_view.setVisibility(View.VISIBLE);
                    last_view.setVisibility(View.GONE);
                    last_text_view.setTextSize(17);
                    last_text_view.setTextColor(Color.rgb(128, 128, 128));
                    last_view = repurchase_volume_button_view;
                    last_text_view = repurchase_volume_button_text_view;
                }
            }
        });

        full_reduction_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (full_reduction_activity_state != 1) {
                    Intent intent = new Intent(Check_All_Activity_Page.this, Full_Reduction_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(Check_All_Activity_Page.this, "滿減活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });

        reduce_the_delivery_fee_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reduce_the_delivery_fee_state != 1) {
                    Intent intent = new Intent(Check_All_Activity_Page.this, Reduce_The_Delivery_Fee_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(Check_All_Activity_Page.this, "减配送费活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });

        discount_product_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (discount_product_state != 1) {
                    Intent intent = new Intent(Check_All_Activity_Page.this, Discount_Product_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(Check_All_Activity_Page.this, "折扣商品活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });

        coupon_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog1 = new Dialog(view.getContext());
                dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog1.setContentView(R.layout.choose_coupon_type_view);

                ImageView close = dialog1.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog1.dismiss();
                    }
                });

                LinearLayout collect_and_have_gifts = dialog1.findViewById(R.id.collect_and_have_gifts);
                collect_and_have_gifts.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_collect_and_have_gifts_page) {
                            Toast.makeText(Check_All_Activity_Page.this, "收藏有礼活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Collect_And_Have_Gifts_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                LinearLayout collect_points_to_return_coupon = dialog1.findViewById(R.id.collect_points_to_return_coupon);
                collect_points_to_return_coupon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_collect_points_to_return_coupon) {
                            Toast.makeText(Check_All_Activity_Page.this, "集点返券活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Collect_Points_To_Return_Coupon_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                LinearLayout return_coupon_for_placing_an_order = dialog1.findViewById(R.id.return_coupon_for_placing_an_order);
                return_coupon_for_placing_an_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (is_open_return_coupon_for_placing_an_order) {
                            Toast.makeText(Check_All_Activity_Page.this, "下单返券活动已创建", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Intent intent = new Intent(dialog1.getContext(), Return_Coupon_For_Placing_An_Order_Page.class);
                            intent.putExtra("email", email);
                            intent.putExtra("shop_name", shop_name);
                            startActivityForResult(intent, 1);
                        }
                    }
                });

                dialog1.show();
                dialog1.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog1.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog1.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        new_custom_receive_a_discount_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (new_custom_receive_a_discount_state != 1) {
                    Intent intent = new Intent(Check_All_Activity_Page.this, New_Customers_Receive_A_Discount_Page.class);
                    intent.putExtra("email", email);
                    startActivityForResult(intent, 1);
                }
                else {
                    Toast.makeText(Check_All_Activity_Page.this, "门店新客立减活动已创建", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 1: {
                set_dialog();
                Data_Thread data_thread = new Data_Thread();
                data_thread.start();
            }
        }
    }
}